/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.mohas.docsearch.model;

import java.io.File;
import java.io.Reader;

import net.mohas.docsearch.Constants;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.LowerCaseFilter;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.id.IndonesianStemFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.util.Version;
/**
 *
 * Class untuk melakukan text operation (operasi teks) dalam bahasa Indonesia
 * dimulai dari tokenization, lowercase huruf, stop words removal, dan stemming
 * menggunakan pustaka Lucene
 * 
 * @author Muhammad, 15 April 2014
 */
public final class CustomIndonesianAnalyzer extends Analyzer {

    @SuppressWarnings("resource")
	@Override
    public TokenStream tokenStream(String string, Reader reader) {

		File stopwordsFile = new File(Constants.RESOURCES_DIR
				+ Constants.STOPWORDS_DIR
				+ Constants.IND_STOPWORDS_FILENAME);
        StopAnalyzer bahasaStopwords = null;
        try {
            bahasaStopwords = new StopAnalyzer(Version.LUCENE_33, stopwordsFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // Tokenisasi menggunakan StandardTokenization
        StandardTokenizer stdTokenizer = new StandardTokenizer(Version.LUCENE_33, reader);

        // Lowercase huruf
        LowerCaseFilter lcFilter = new LowerCaseFilter(Version.LUCENE_33, stdTokenizer);

        // stopwords removal
        StopFilter stopFilter = new StopFilter(Version.LUCENE_33, lcFilter, bahasaStopwords.getStopwordSet(), false);

        IndonesianStemFilter stemmer = new IndonesianStemFilter(stopFilter);
        return stemmer;
    }

}
